package com.xinyue.fishinghunter.center.service;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mygame.common.utils.GameTimeUtil;
import com.xinyue.center.messages.request.LoginParam;
import com.xinyue.center.messages.response.VoPlayerBasic;
import com.xinyue.center.messages.response.VoUserAccount;
import com.xinyue.center.service.AbstractUserLoginService;
import com.xinyue.fishinghunter.dao.FishingHunterAccountDao;
import com.xinyue.fishinghunter.dao.entity.FishingHunterAccount;
import com.xinyue.fishinghunter.dao.entity.PlayerInfo;
import com.xinyue.framework.dao.entity.PlayerBasic;
import com.xinyue.framework.dao.entity.UserBasic;

@Service
public class UserLoginService extends AbstractUserLoginService {

	@Autowired
	private FishingHunterAccountDao fishingHunterAccountDao;

	@Override
	public void addPlayer(long userId, PlayerBasic player) {
		FishingHunterAccount fishingHunterAccount = fishingHunterAccountDao.findByIdFromCacheOrLoader(userId)
				.orElse(null);
		if (fishingHunterAccount != null) {
			PlayerInfo playerInfo = new PlayerInfo();
			playerInfo.setNickname(player.getNickName());
			playerInfo.setPlayerId(player.getPlayerId());
			fishingHunterAccount.getPlayerInfos().add(playerInfo);
			fishingHunterAccountDao.saveOrUpdate(fishingHunterAccount, userId);
		}
	}

	@Override
	protected UserBasic getUserAccount(String userName) {
		long userId = this.getUserIdByUserName(userName);
		if(userId > 0) {
			return fishingHunterAccountDao.findByIdFromCacheOrLoader(userId).orElse(null);
		}
		return null;
	}

	@Override
	protected boolean isAutoRegiter() {
		return true;
	}

	@Override
	protected VoUserAccount loginSuccess(UserBasic account) {
		FishingHunterAccount fishingHunterAccount = (FishingHunterAccount) account;
		VoUserAccount voUserAccount = new VoUserAccount();
		voUserAccount.setCreateTime(account.getCreateTime());
		voUserAccount.setUserName(account.getAccount());
		voUserAccount.setUserId(account.getUserId());
		List<VoPlayerBasic> players = fishingHunterAccount.getPlayerInfos().stream().map(a->{
			VoPlayerBasic playerBasic = new VoPlayerBasic();
			playerBasic.setPlayerId(a.getPlayerId());
			playerBasic.setNickName(a.getNickname());
			return playerBasic;
		}).collect(Collectors.toList());
		voUserAccount.setPlayers(players);
		return voUserAccount;
	}

	@Override
	protected UserBasic createRegister(LoginParam loginParam, long userId) {
		FishingHunterAccount fishingHunterAccount = new FishingHunterAccount();
		fishingHunterAccount.setAccount(loginParam.getUserName());
		fishingHunterAccount.setCreateTime(GameTimeUtil.currentTimeMillis());
		fishingHunterAccount.setPassword(loginParam.getPassword());
		fishingHunterAccount.setRegisterIp(loginParam.getLoginIp());
		fishingHunterAccount.setUserId(userId);
		return fishingHunterAccount;
	}

	@Override
	protected UserBasic getUserFromDBByAccount(String account) {
		
		return fishingHunterAccountDao.findByAccount(account);
	}

	@Override
	public void updateAccount(UserBasic userBasic) {
		FishingHunterAccount account = (FishingHunterAccount) userBasic;
		fishingHunterAccountDao.saveOrUpdate(account, userBasic.getUserId());
	}

}
